package com.mak.menoy.config;

import android.content.Context;
import android.text.TextUtils;

import com.mak.menoy.R;
import com.mak.menoy.config.DialogTask.DialogMode;
import com.mak.menoy.request.BaseHttpRequest;
import com.mak.menoy.response.BaseHttpResponse;
import com.mak.menoy.util.NetWorkState;

public class DialogTaskExcutor {

/**
 *
 * @author Bie Sai Nan
 */
	private static final int MAX_FAILURES = 0;
	private int mFailures = 0;
	private Context mContext;
	private static final String TAG="DialogTaskExcutor";

	private  BaseHttpRequest mRequest;
	private TaskResultPicker mTaskResultPicker;

	private boolean showIncorrectMsg;

	
	DialogTaskExcutor(Context context, BaseHttpRequest request,
			TaskResultPicker taskResultPicker) {
		this(context, request, taskResultPicker, DialogTask.DialogMode.NO_TEXT);
	}

	DialogTaskExcutor(Context context, BaseHttpRequest request, TaskResultPicker taskResultPicker,DialogTask.DialogMode mode) {
		super();
		this.mContext = context;
		this.mRequest = request;
		this.mTaskResultPicker = taskResultPicker;

		boolean showExtraMsg = (mode == DialogMode.HIDDEN ? false : true);
		mTaskResultPicker.setShowErrorMessage(showExtraMsg);
		mTaskResultPicker.setShowIncorrectMessage(showExtraMsg);
		showIncorrectMsg = showExtraMsg;

//	    if (!netWorkStatu) {
//			mTaskResultPicker.onError(100);
//		}else{
			executeTask(mode);
//		}
		
	}

	private void executeTask(final DialogTask.DialogMode mode) {
		DialogTask task = new DialogTask(mContext) {
			@Override
			public void doStuffWithResult(Object result) {
				if (mTaskResultPicker != null) {
					if(result!=null){
						if (result instanceof Integer) {
								mTaskResultPicker.onError((Integer) result);
						}else{
							
							if (((BaseHttpResponse) result).getHeader().getMsgcode() == 104) {
								mTaskResultPicker.onError(104);
							}
							
							if (((BaseHttpResponse) result).getHeader().getResult() == BaseHttpResponse.CORRECT_CODE) {
								mTaskResultPicker.doStuffWithCorrectResult(result);
							} else {
								String errorMsg = ((BaseHttpResponse) result).getHeader().getMsgtips();
								mTaskResultPicker.doStuffWithIncorrectResult(errorMsg,mRequest);
								ToastUtils.longToast(MakeApplicatiaon.getInstance(), errorMsg);
							}
								
						}
					}
				}
			}

			@Override
			protected void doNoResultReturned() {
				super.doNoResultReturned();
			 if (mTaskResultPicker != null)
					mTaskResultPicker.doStuffWithNoResult();
			}

		};
		task.setMode(mode);
		task.execute(mRequest);
	}

	public abstract static class TaskResultPicker {
		private boolean showIncorrectMsg;
		private boolean showErrorMsg = true;
		public  final static Integer CODE_NOT_FOUND = 404;
		public  final static Integer CODE_NOT_ACCEPT = 403;
		public  final static Integer CODE_SERVER_ERROR = 500;
		public  final static Integer CODE_NO_INTERNET = -1;
		public  final static Integer CODE_PARSER_ERROR = 110;
		public  final static Integer CODE_REQUEST_TIME_OUT_ERROR = 119;

		public abstract void doStuffWithCorrectResult(Object result);

		public void doStuffWithIncorrectResult(String errorMsg,BaseHttpRequest request) {
			if (showIncorrectMsg){
					if(!TextUtils.isEmpty(errorMsg)){
						
						
					}
			
				}
		}	
		public void onError(Integer errorCode) {
		}

		public void doStuffWithNoResult() {
			if (showErrorMsg)
				if (!NetworkChecker.isNetworkAvailable(MakeApplicatiaon
						.getInstance()))
					ToastUtils.longToast(MakeApplicatiaon.getInstance(),
							R.string.network_not_available);
				else
					ToastUtils.longToast(MakeApplicatiaon.getInstance(),
							R.string.load_faild);
		}

		public final void setShowIncorrectMessage(boolean showIncorrectMsg) {
			this.showIncorrectMsg = showIncorrectMsg;
		}

		public final void setShowErrorMessage(boolean showErrorMsg) {
			this.showErrorMsg = showErrorMsg;
		}

	}
	
	
	public static DialogTaskExcutor executeTask(Context context,
			BaseHttpRequest request, TaskResultPicker taskResultPicker) {
		return new DialogTaskExcutor(context, request, taskResultPicker);
	}

	public static DialogTaskExcutor executeTask(Context context, BaseHttpRequest request,
			TaskResultPicker taskResultPicker,DialogMode dialogMode) {
		return new DialogTaskExcutor(context, request, taskResultPicker,dialogMode);
	}

}
